package cn.hdr.train.lc;

/**
 * 给定 nums = [-2, 0, 3, -5, 2, -1]，求和函数为 sumRange()
 *
 * sumRange(0, 2) -> 1
 * sumRange(2, 5) -> -1
 * sumRange(0, 5) -> -3
 *
 */
public class NumArray {
    private int[] allNumAr = null;

    public NumArray(int[] nums) {
        allNumAr = new int[nums.length];
        if(nums.length >= 1)
            allNumAr[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            allNumAr[i] = allNumAr[i - 1] + nums[i];
        }
    }

    public int sumRange(int i, int j) {
        if(i == 0)
            return allNumAr[j];

        return allNumAr[j] - allNumAr[i - 1];
    }

}